package com.hsbc.dev.teamo4.sms.common.utils.data;

import java.util.HashSet;

/**
 * @author:Cady
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.util
 * @Description:  用于构建树结构所用的节点类
 * @Date date: 2018-07-26
 */
public class TreeNode<T> {
    /**
     * 节点包含的数据，为对应模板T的对象
     */
    private T value;
    /**
     * 子节点
     */
    private HashSet<TreeNode> subNotes;
    /**
     * 父节点
     */
    private TreeNode parent;

    public TreeNode() {
        subNotes = new HashSet<TreeNode>();
    }

    public TreeNode(T value) {
        this();
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public HashSet<TreeNode> getSubNotes() {
        return subNotes;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public void setValue(T value) {
        this.value = value;
    }


    public void setSubNotes(HashSet<TreeNode> subNotes) {
        this.subNotes = subNotes;
    }

    /**
     * @Description 将子节点添加到set中，并将子节点的父节点赋值为当前节点
     * @Date: 15:35 2018-07-27
     * @Param node
     * @return void
     */
    public void addSubNote(TreeNode node){
        this.subNotes.add(node);
        node.setParent(this);
    }

    public void deleSubNote(TreeNode node){
        this.subNotes.remove(node);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        TreeNode<?> treeNode = (TreeNode<?>) o;

        if (value != null ? !value.equals(treeNode.value) : treeNode.value != null) {
            return false;
        }
        return (subNotes != null ? subNotes.equals(treeNode.subNotes) : treeNode.subNotes == null) && (parent != null ? parent.equals(treeNode.parent) : treeNode.parent == null);
    }

    @Override
    public int hashCode() {
        int result = value != null ? value.hashCode() : 0;
        result = 31 * result + (subNotes != null ? subNotes.hashCode() : 0);
        result = 31 * result + (parent != null ? parent.hashCode() : 0);
        return result;
    }
}
